﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{

	public class QuanDAO:IData<Quan> 
 	{
		public QuanDAO(String myconnecString) : base(myconnecString) { }

		protected override int InsertOnSubmit(ref Quan item, int t) {
			 SqlParameter p = new SqlParameter(); 
			 if( t == 0)
				p.Direction = ParameterDirection.Output;
			 p.ParameterName = "@" + Quan.c_id;
			 p.Value = item.id;
			 Object obj = item.id; 
			int i = connec.ExecuteNonQuery("sp_Quan", CommandType.StoredProcedure,p,ref obj,new SqlParameter("@flag", t),
				 new SqlParameter("@" + Quan.c_tinhID, item.tinhID), new SqlParameter("@" + Quan.c_TenQH, item.TenQH));
			if(t == 0)
				item.id = Convert.ToInt32(obj);
			return i;
		}

		public override Quan GetItem(IDataReader dr)
		{
			Quan obj = new Quan();
			obj.id = Convert.ToInt32(dr[Quan.c_id]);
			obj.tinhID = Convert.ToInt32(dr[Quan.c_tinhID]);
			obj.TenQH = Convert.ToString(dr[Quan.c_TenQH]);
			return obj;
		}

		public override List<Quan> GetAll()
		{
			return GetAll("sp_GetAllQuan", CommandType.StoredProcedure);
		}

		public override Quan GetItemById(object id)
		{
			return GetAll().SingleOrDefault(p => p.id == Convert.ToInt32(id));
		}
		public override Quan GetItemById(Quan item){
			throw new NotImplementedException();
		}

	}

 }
